<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/5 15:19
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Wx_Model extends ADM_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 获取设置
     * @return bool|mixed
     */
    public function getSetting($key)
    {
        $db = clone($this->db);
        $where = [
            'use' => 'wx',
            'key' => $key
        ];

        $q = $db->get_where('setting', $where);
        $row = $q->row_array();
        if ($row) {
            $result = json_decode($row['value'], true);
            return $result;
        }
        return false;
    }

    /**
     * @param $data
     */
    public function saveSetting($data, $key)
    {
        $db = clone($this->db);
        $where = [
            'use' => 'wx',
            'key' => $key
        ];

        $data = json_encode($data);

        $q = $db->get_where('setting', $where);
        if ($row = $q->row_array()) {
            $db->where($where);
            $db->update('setting', ['value' => $data]);
            $result = $db->affected_rows() ? true : false;
        } else {
            $db->insert('setting', array_merge($where, ['value' => $data]));
            $result = $db->insert_id() ? true : false;
        }

        return $result;
    }

}
